var TableNavs = function () {
    return {
        init: function () {
			function restoreRow(oTable, nRow) {
                var aData = oTable.fnGetData(nRow);
                var jqTds = $('>td', nRow);
				//oTable.fnUpdate(aData['friendlinkId'], nRow,0, false);
				oTable.fnUpdate(aData['navTitle'], nRow,1, false);				
				oTable.fnUpdate(aData['navUrl'], nRow,2, false);
				oTable.fnUpdate(aData['navType'], nRow,3, false);
				oTable.fnUpdate(aData['navSort'], nRow,4, false);
				
				oTable.fnUpdate('<a class="edit btn btn-xs default" href=""><i class="fa fa-edit"></i> 修改</a></a><a class="delete btn btn-xs red" href=""><i class="fa fa-trash-o"></i> 删除</a>', nRow, 7, false);
                oTable.fnDraw();
            }
			
            function editRow(oTable, nRow,isNew) {
				var isNew=isNew?isNew:'';
                var aData = oTable.fnGetData(nRow);
                var jqTds = $('>td', nRow);
				var html = '<select class="form-control input-small">';
					html +='<option value="0" selected>首页头部</option>';
					html +='<option value="1" >首页底部</option>';
					html +='<option value="2" >买家中心</option>';
					html +='<option value="3" >卖家中心</option>';
					html +='</select>';
					
				jqTds[3].innerHTML = html;
				
				var jqOpts = $('option');
				for (var i = 0, iLen = jqOpts.length; i < iLen; i++) {
					//先清空
					jqOpts.eq(i).attr('selected',false);					
					if(aData['navType']==jqOpts.eq(i).val()){
						jqOpts.eq(i).attr('selected',true);						
					}
				} 
                jqTds[1].innerHTML = '<input type="text" class="form-control input-small" value="' + aData['navTitle'] + '">';
                jqTds[2].innerHTML = '<input type="text" class="form-control input-small" value="' + aData['navUrl'] + '">';
                //jqTds[3].innerHTML = '<input type="text" class="form-control input-small" value="' + aData['navType'] + '">';
				jqTds[4].innerHTML = '<input type="text" class="form-control input-small" value="' + aData['navSort'] + '">';
				jqTds[7].innerHTML = '<a class="edit btn btn-xs green" href="" name="Save"><i class="fa fa-check"></i> 保存</a></a><a class="cancel btn btn-xs default" '+isNew+' href=""><i class="fa fa-times"></i> 取消</a>';
			}

			

            function saveRow(oTable, nRow) {
				var	jqSelects = $('select', nRow);
				oTable.fnUpdate(jqSelects[0].value, nRow, 3, false);
				
                var jqInputs = $('input', nRow);
				oTable.fnUpdate(jqInputs[0].value, nRow, 1, false);
                oTable.fnUpdate(jqInputs[1].value, nRow, 2, false);
                oTable.fnUpdate(jqInputs[2].value, nRow, 4, false);
                //oTable.fnUpdate(jqInputs[3].value, nRow, 4, false);
                //oTable.fnUpdate(jqInputs[4].value, nRow, 4, false);
                oTable.fnUpdate('<a class="edit btn btn-xs default" href=""><i class="fa fa-edit"></i> 修改</a></a><a class="delete btn btn-xs red" href=""><i class="fa fa-trash-o"></i> 删除</a>', nRow, 7, false);
                oTable.fnDraw();
            }
			
			
			         
            oTable = $('#table_common').dataTable({
                "aLengthMenu": [
                    [-1,5, 15, 20],
                    ["全部",'5条', '15条', '20条'] 
                ],
                "iDisplayLength": -1,
				"serverSide":true,
				//"aaSorting":[[6,'desc']],
                "sPaginationType": "bootstrap",
				//"bStateSave": true, 
                "oLanguage": {
					"sSearch":"搜索",
                    "sLengthMenu": "每页显示 _MENU_ 记录",    
					"sEmptyTable":"没有数据",
					"sInfo": "当前数据为从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录",   
					"sInfoEmpty": "没有数据",   
					"sInfoFiltered" : "",
					"sInfoPostFix" : "",
					"sLoadingRecords":"",
					"sProcessing": "正在加载数据...", 
					"sZeroRecords": "没有检索到数据", 
                    "oPaginate": {   
						"sFirst": "首页",   
						"sPrevious": "上一页",   
						"sNext": "下一页",   
						"sLast": "尾页"  
					}   
                },
				"sAjaxSource":ajaxUrl,
				"sAjaxDataProp": "data",
				
				"aoColumns": [
	
					{"sTitle":'序号',"mDataProp": 'id'},
					{"sTitle":'标题',"mDataProp": 'navTitle'},
					{"sTitle":'链接地址',"mDataProp": 'navUrl'},
					{"sTitle":'类型',"mDataProp": 'navType'},
					{"sTitle":'排序',"mDataProp": 'navSort'},
					{"sTitle":'新窗口打开',"mDataProp": 'isOpen'},
					{"sTitle":'是否显示',"mDataProp": 'isShow'},
					{"sTitle":'操作',"mDataProp": 'createTime'}
				],
                "aoColumnDefs": [{
                        'bSortable': false,
						'aTargets': ['_all']
                },
				{"aTargets":[0],"sClass":'text-center',"sWidth":'5%'},
				{"aTargets":[1],"mRender":function(data,type,full){
					if(full['navType']==0 || full['navType']==2){
						return '<strong>'+data+'</strong>';
					}else{
						return data;
					}
				}},
				{"aTargets":[3],"mRender":function(data,type,full){						
					if(data==0){							
						return '<span class="btn btn-xs blue">首页头部</span>';
					}else if(data==1){
						return '<span class="btn btn-xs green">首页底部</span>';
					}else if(data==2){
						return '<span class="btn btn-xs blue">买家中心</span>';
					}else{
						return '<span class="btn btn-xs green">卖家中心</span>';
					}
                    },"sClass":'text-center',"sWidth":'5%'},
				{"aTargets":[5],"mRender":function(data,type,full){						
					if(data==1){							
						return '<a href="#" class="isOpen btn btn-xs blue">是</a>';
					}else{
						return '<a href="#" class="isOpen btn btn-xs default">否</a>';
					}						
                    },"sClass":'text-center',"sWidth":'5%'},
				{"aTargets":[6],"mRender":function(data,type,full){						
					if(data==1){							
						return '<a href="#" class="isShow btn btn-xs blue">显示</a>';
					}else{
						return '<a href="#" class="isShow btn btn-xs default">隐藏</a>';
					}						
                    },"sClass":'text-center',"sWidth":'5%'},
				{"aTargets":[7],"mRender":function(data,type,full){
					return '<a class="edit btn btn-xs default" href=""><i class="fa fa-edit"></i> 修改</a><a class="delete btn btn-xs red" href=""><i class="fa fa-trash-o"></i> 删除</a>';
                    },"sClass":'text-center',"sWidth":'5%'}
				],
				"fnCreatedRow":function(nRow,aData,iDataIndex){
					$('td:eq(0)',nRow).html(iDataIndex+1);//为第一个单元格增加序号
				}				
            });

            jQuery('#table_common_wrapper .dataTables_filter input').addClass("form-control input-medium input-inline"); // modify table search input
            jQuery('#table_common_wrapper .dataTables_length select').addClass("form-control input-small"); // modify table per page dropdown
            jQuery('#table_common_wrapper .dataTables_length select').select2({
                showSearchInput : false 
            }); 
			
			var nEditing = null;

			$('#table_common_new').click(function (e) {
                e.preventDefault();
				if (nEditing !== null) {
                    layer.confirm('确定不保存数据添加新行？',function(index){
						if($(nEditing).find("a[data-mode='new']").length>0){	
							oTable.fnDeleteRow(nEditing);	
						}else{
							restoreRow(oTable, nEditing);
						}
						var aiNew = oTable.fnAddData({"id":'', "navTitle":'', "navUrl":'',"navType":'',"navSort":'',"isOpen":'',"isShow":'',
							"createTime":'<a class="edit btn btn-xs green" href="" name="Save"><i class="fa fa-check"></i> 保存</a></a><a class="cancel btn btn-xs default" data-mode="new" href=""><i class="fa fa-times"></i> 取消</a>'					
						});
						var nRow = oTable.fnGetNodes(aiNew[0]);
						editRow(oTable, nRow,"data-mode='new'");
						nEditing = nRow;
						layer.close(index);
					});
                }else{
					var aiNew = oTable.fnAddData({"id":'', "navTitle":'', "navUrl":'',"navType":'',"navSort":'',"isOpen":'',"isShow":'',
						"createTime":'<a class="edit btn btn-xs green" href="" name="Save"><i class="fa fa-check"></i> 保存</a></a><a class="cancel btn btn-xs default" data-mode="new" href=""><i class="fa fa-times"></i> 取消</a>'					
					});
					var nRow = oTable.fnGetNodes(aiNew[0]);
					editRow(oTable, nRow,"data-mode='new'");
					nEditing = nRow;
				}    
            });
			//是否新窗口打开
			$('#table_common a.isOpen').live('click', function (e) {
                e.preventDefault();
                var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				var isOpen = aData['isOpen']==1?0:1;
				$.post(ajaxUrl,{id:aData['id'],isOpen:isOpen,ajax:'isOpen'},function(data){
					if(data){
						layer.msg('导航状态修改成功！',{icon: 1,time: 2000});
						oTable.fnUpdate(isOpen, nRow,5, false);
						oTable.fnDraw();
					}else{
						layer.msg('导航状态未修改！',{icon: 2,time: 2000});
						return;
					}
			   });
            });
			
			//是否显示
			$('#table_common a.isShow').live('click', function (e) {
                e.preventDefault();
                var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				var isShow = aData['isShow']==1?0:1;
				$.post(ajaxUrl,{id:aData['id'],isShow:isShow,ajax:'isShow'},function(data){
					if(data){
						layer.msg('导航状态修改成功！',{icon: 1,time: 2000});
						oTable.fnUpdate(isShow, nRow,6, false);
						oTable.fnDraw();
					}else{
						layer.msg('导航状态未修改！',{icon: 2,time: 2000});
						return;
					}
			   });
            });
			
			//删除
            $('#table_common a.delete').live('click', function (e) {
				e.preventDefault();
				var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				layer.confirm('确定删除该导航？',function(index){
					$.post(ajaxUrl,{id:aData['id'],ajax:'delNavs'},function(data){
						if(data){
							layer.msg('导航删除成功！',{icon: 1,time: 2000});
							oTable.fnDeleteRow(nRow);
							
						}else{
							layer.msg('导航删除失败！',{icon: 2,time: 2000});
							return;
						}
				   });					
				});	
                
            });

            $('#table_common a.cancel').live('click', function (e) {
                e.preventDefault();
				var nRow = $(this).parents('tr')[0];
                if ($(this).attr("data-mode") == "new") {
                    
                    oTable.fnDeleteRow(nRow);
                } else {
                    restoreRow(oTable, nRow);
                    //nEditing = null;
                }
				nEditing = null;
            });

            $('#table_common a.edit').live('click', function (e) {
                e.preventDefault();
                var nRow = $(this).parents('tr')[0];
                if (nEditing !== null && nEditing != nRow) {
                    layer.confirm('确定不保存数据编辑新行？',function(index){
						if($(nEditing).find("a[data-mode='new']").length>0){	
							oTable.fnDeleteRow(nEditing);	
						}else{
							restoreRow(oTable, nEditing);
						}
						editRow(oTable, nRow);
						nEditing = nRow;
						layer.close(index);
					});
                } else if (nEditing == nRow && this.name == "Save") {
                    /* Editing this row and want to save it */
                    saveRow(oTable, nEditing);
					
					var aData = oTable.fnGetData(nRow);
					if(aData['id']){
						aData['ajax']="editNavs";
					}else{
						aData['ajax']="addNavs";
					}
					//alert(aData);
					$.ajax({
						url: ajaxUrl,
						type: 'POST',
						dataType: 'json',
						data: aData,
						success : function(data) {
							if(data.status=='1'){
								layer.msg(data.info, 
									{icon: 1,time: 2000},
									function(){
									nEditing = null;
									oTable.fnReloadAjax();
									}
								);   	
							}else{
								layer.msg(data.info, 
									{icon: 2,time: 2000}
								);   
								return;
							}
						}
					});

                } else {
                    /* No edit in progress - let's start one */
                    editRow(oTable, nRow);
                    nEditing = nRow;
                }
            });
        }

    };

}();